
import { touches } from '@/utils/touches'
import {setQuery } from '@/utils/auth'
import { getQiniuUploadToken_ } from '@/api/customerService'
import * as qiniu from 'qiniu-js'
import { uuid } from '@/utils/uuid'
export default {

    created () {
        setQuery(this.$router.history.current.query)
    },
    mounted () {
        touches();
    },
    methods: {

        filesSubmitted(f) {
            this.loadingMsg="图片处理中"
            this.showLoading = true
            this.showChargeOrder = false
            let PicArr = f.file.name.split('.')
            let str1= PicArr[PicArr.length-1]

            getQiniuUploadToken_().then(res => {
                if (res.status === 0) {
                    let data = new FormData()
                    data.append('file', f.file)
                    this.filename.push(`${uuid()}.${str1}`)
                    data.append('key', this.filename[this.filename.length -1])
                    data.append('token',res.data.token)

                    var observable = qiniu.upload(f.file, this.filename[this.filename.length -1], res.data.token)
                    let that = this

                    var observer = {
                        next(res){
                            //接收上传进度信息
                            that.loadingMsg = '图片处理中'+ parseInt(res.total.percent) + '%'
                        },
                        error(err){
                            //处理错误情况，提示用户该图片上传失败
                            that.showLoading = false
                            if(that.filename.length > 0){
                                that.filename.pop()
                                that.fileError()
                            }
                        },
                        complete(res){
                            //上传完成
                            that.filenameNew.push(res.key)
                            that.showLoading = false
                        }
                    }
                    observable.subscribe(observer) // 上传开始
                }
            })  },
        filesAdded (files) {
            let hasIgnore = false
            const maxSize = 10 * 1024 * 1024 // 10M
            for (let k in files) {
                const file = files[k]
                if (file.size > maxSize) {
                    file.ignore = true
                    hasIgnore = true
                }
            }
            hasIgnore && this.$createToast({
                type: 'warn',
                time: 2000,
                txt: '选择的文件不能大于10M'
            }).show()
        },
        filesSuccess(file){ },
        fileError(){
            this.$createToast({
                type: 'warn',
                time: 2000,
                txt: '选择文件失败，请删除后重新选择文件'
            }).show()
        },
        showDateTimePicker () {
            if (!this.dateTimePicker) {
                this.dateTimePicker = this.$createDatePicker({
                    title: '请选择时间',
                    min: new Date(2018, 7, 8, 8, 0),
                    max: new Date(2022, 9, 20, 20, 59),
                    value: new Date(),
                    columnCount: 5,
                    format: {
                        year: 'YYYY年',
                        month: 'MM月',
                        date: 'DD日',
                        hour: 'hh时',
                        minute: 'mm分'
                    },
                    onSelect: (date, selectedVal) => this.selectHandle(date, selectedVal)
                })
            }
            this.dateTimePicker.show()
        },
    },
}
